package com.controller;

import cn.hutool.core.date.DateTime;
import com.bean.Clazz;
import com.bean.Student;
import com.bean.User;
import com.service.UserService;
import com.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Date;
import java.util.List;

/**
 * @author 末日流光
 * @ClASSNAME UserConroller
 * @PACKAGE_NAME com.controller
 * @date 2022/5/12 21:21
 * @project SSM-demo1
 * @company dzy
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    /**
     *
     * 管理员登录
     * @param username
     * @param password
     * @param session
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:07
     */

    @RequestMapping(value = "/adminlogin")
    @ResponseBody
    public ResultVo adminlogin(@RequestParam("username") String username,@RequestParam("password") String password, HttpSession session){
        ResultVo resultVo=new ResultVo();
        if("".equals(username)||username==null||"".equals(password)||password==null){
            resultVo.setMsg("用户名密码为空，请输入");

            return resultVo;
        }else{
            User user=userService.adminlogin(username,password);

            if(user==null){
                resultVo.setMsg("用户名密码错误，请重新输入");
                return resultVo;
            }else{
                session.setAttribute("adminUser",user);
                resultVo.setMsg("登录成功");
                return resultVo;
            }
        }
    }
    /**
     *
     * 退出登录
     * @param request
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:07
     */

    @RequestMapping(value = "/ExitServlet")
    public String ExitServlet(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.removeAttribute("adminuser");
        return "redirect:/";
    }
    /**
     *
     * 用户信息页面
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:07
     */

    @RequestMapping(value = "/admin/user_list_admin")
    public String user_list_admin(){
        return "user/user_list_admin";
    }

    /**
     *
     * 分页查询用户信息
     * @param page
     * @param limit
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:07
     */

    @RequestMapping(value = "/admin/AdminUserListServlet")
    @ResponseBody
    public ResultVo AdminUserListServlet(int page, int limit){
        ResultVo resultVo = new ResultVo();
        List<User> users=userService.getUsersByPage(page,limit);
        int length = userService.getUserLength();
        resultVo.setData(users);
        resultVo.setCount(length);
        resultVo.setMsg("success");
        return resultVo;
    }
    /**
     *
     * 跳转到添加管理员信息页面
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:08
     */

    @RequestMapping(value = "/admin/AdminAddUserServlet1")
    public String AdminAddUserServlet1(){
        return "user/add_user";
    }
    /**
     *
     * 添加管理员信息
     * @param user
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:08
     */

    @RequestMapping(value = "/admin/AdminAddUserServlet2")
    public String AdminAddUserServlet2(User user, Model model){
//        System.out.println(user);
        Date date = new Date();
        DateTime time = new DateTime(date);
        user.setUserRegtime(time.toString());
        userService.addUser(user);
        model.addAttribute("users", userService.getUsers());
        return "main";
    }

    /**
     *
     * 根据管理员id删除管理员信息
     * @param user_id
     * @param model
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:08
     */

    @RequestMapping(value = "/admin/delUserServlet")
    @ResponseBody
    public ResultVo delUserById(@RequestParam("user_id") int user_id, Model model){
        ResultVo resultVo = new ResultVo();
        int i=userService.delUserById(user_id);;
        model.addAttribute("users", userService.getUsers());
        if(i==1){
            resultVo.setMsg("删除成功");}
        else{
            resultVo.setMsg("删除失败");
        }
        return resultVo;
    }
    /**
     *
     * 跳转到编辑管理员信息页面
     * @param user_id
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:09
     */

    @RequestMapping(value = "/admin/AdmineditUserServlet")
    public String AdmineditUserServlet(@RequestParam("user_id") int user_id,Model model){
//        System.out.println(user_id);
        User user=userService.selectUserById(user_id);
        model.addAttribute("user",user);
        return "user/edit_user";
    }
    /**
     *
     * 编辑管理员信息
     * @param user
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:10
     */

    @RequestMapping(value = "/admin/AdmineditUserServlet2")
    @ResponseBody
    public ResultVo AdmineditUserServlet2(User user){
        Date date = new Date();
        DateTime time = new DateTime(date);
        user.setUserRegtime(time.toString());
        ResultVo resultVo = new ResultVo();
        int i=userService.updateUserById(user);
//        System.out.println(i);
        if(i==1){
            resultVo.setMsg("编辑成功");}
        else{
            resultVo.setMsg("编辑失败");
        }
        return resultVo;
    }
    /**
     *
     * 批量删除管理员信息
     * @param pIds
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:10
     */

    @RequestMapping("/admin/deleteUserBatch")
    @ResponseBody
    public ResultVo deleteBatch(@RequestParam("pIds") String[] pIds) {
        ResultVo resultVo = new ResultVo();
        for(String id:pIds){
            userService.delUserById(Integer.parseInt(id));
        }
        resultVo.setData(pIds);
        resultVo.setMsg("删除成功");
        return resultVo;
    }
}

